package lol.clann.minecraft.springboot.plugin.bukkit.impl;

import lol.clann.minecraft.springboot.api.bukkit.model.ProjectileHolder;
import lol.clann.minecraft.springboot.api.bukkit.utils.ProjectileUtils;
import lol.clann.minecraft.springboot.api.context.SpringContext;
import lol.clann.minecraft.springboot.api.model.core.ClassScanner;
import relocate.com.google.common.collect.ImmutableMap;
import org.bukkit.entity.Projectile;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author pyz
 * @date 2019/6/7 12:07 AM
 */
@Service
public class ProjectileUtilsImpl implements ProjectileUtils {

    private Map<String, ProjectileHolder> projectileMap = new HashMap<>();

    @PostConstruct
    private void init() throws ClassNotFoundException {
        ClassScanner scanner = new ClassScanner(SpringContext.getApplicationEnvironment());
        scanner.addIncludeFilter(new AssignableTypeFilter(Projectile.class));
        Set<String> set = scanner.doScan("org.bukkit");
        for (String className : set) {
            Class cls = Class.forName(className);
            if (Modifier.isAbstract(cls.getModifiers()) || Modifier.isInterface(cls.getModifiers())) {
                continue;
            }
            String name = cls.getSimpleName().replace("Craft", "");
            projectileMap.put(name, new ProjectileHolder(cls));
        }
        projectileMap = ImmutableMap.copyOf(projectileMap);
    }

    @Override
    public Map<String, ProjectileHolder> getProjectileMap() {
        return projectileMap;
    }
}
